package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.TwoLevelChildForPopWindowEntity;
import com.sgb.kjwl.model.entity.TwoLevelForPopWindowEntity;
import com.sgb.kjwl.view.ui.adapter.TwoLevelForPopChildWindowAdapter;
import com.sgb.kjwl.view.ui.adapter.TwoLevelForPopWindowAdapter;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.ArrayList;
import java.util.List;

public class TwoLevelEntityPopWindow<D extends TwoLevelForPopWindowEntity> extends BasePopWindow<TwoLevelChildForPopWindowEntity<List<D>>> {

    private TwoLevelForPopWindowAdapter<D> mOneLevel;
    private TwoLevelForPopChildWindowAdapter<D> mTwoLevel;
    private List<TwoLevelChildForPopWindowEntity<List<D>>> mData;
    private TwoLevelClick<D> mTwoLevelClick;
    private int mOneLevelPosition,mTwoLevelPosition;
    private String mSelectName;

    public TwoLevelClick<D> getmTwoLevelClick() {
        return mTwoLevelClick;
    }

    public void setmTwoLevelClick(TwoLevelClick<D> mTwoLevelClick) {
        this.mTwoLevelClick = mTwoLevelClick;
    }

    public TwoLevelEntityPopWindow(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_select_two_level_entity;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        try {
            RecyclerView oneLevel = holder.getPop().findViewById(R.id.rl_one);
            mOneLevel = new TwoLevelForPopWindowAdapter<>(mContext, null);
            mOneLevel.setItemSelect(true);
            oneLevel.setAdapter(mOneLevel);
            mOneLevel.setOnItemClick(new AdapterOnItemClick<TwoLevelChildForPopWindowEntity<List<D>>>() {
                @Override
                public void onItemClick(TwoLevelChildForPopWindowEntity<List<D>> listTwoLevelEntityChildForPopWindow, int position) {
                    mOneLevelPosition = position;
                    mSelectName = listTwoLevelEntityChildForPopWindow.getName();
                    mTwoLevelClick.oneClick(listTwoLevelEntityChildForPopWindow,position);
                    if (listTwoLevelEntityChildForPopWindow.getChildren() != null) {
                        mTwoLevel.setDatas(listTwoLevelEntityChildForPopWindow.getChildren());
                    }
                    mTwoLevel.setDefItem(0);
                }
            });

            RecyclerView twoLevel = holder.getPop().findViewById(R.id.rl_two);
            mTwoLevel = new TwoLevelForPopChildWindowAdapter<>(mContext, null);
            mTwoLevel.setItemSelect(true);
            twoLevel.setAdapter(mTwoLevel);
            mTwoLevel.setOnItemClick(new AdapterOnItemClick<D>() {
                @Override
                public void onItemClick(D d, int position) {
                    mTwoLevelPosition = position;
                    mSelectName = d.getName();
                    mTwoLevelClick.twoClick(d, position);
                }
            });

            TextView tvOk = holder.getPop().findViewById(R.id.tv_ok);
            tvOk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onResultClick.result(mOneLevelPosition, mTwoLevelPosition, mSelectName);
                    dismiss();
                }
            });

            TextView tvReset = holder.getPop().findViewById(R.id.tv_re_set);
            tvReset.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mTwoLevel.setDatasMayNull(null);
                    mTwoLevel.setDefItem(0);
                    mTwoLevel.resetCheckDatas();
                    mOneLevelPosition = 0;
                    mTwoLevelPosition = -1;
                    mSelectName = "";
                    oneLevel.smoothScrollToPosition(0);
                    mOneLevel.setDefItem(0);
                    mOneLevel.setCheckDefault(0);
                    mTwoLevelClick.oneClick(mOneLevel.getDatas().get(0), 0);
                    mTwoLevelClick.reset();
                }
            });
        }catch (Exception e){
            e.fillInStackTrace();
        }
    }

    @Override
    public void setData(List<TwoLevelChildForPopWindowEntity<List<D>>> data) {
        mData = data;

        try {
            if (data != null) {
                TwoLevelChildForPopWindowEntity<List<D>> two = new TwoLevelChildForPopWindowEntity<>();
                two.setName("不限");
                two.setId("");
                two.setNo("");
                two.setChildren(new ArrayList<>());
                mData.add(0,two);
                for (TwoLevelChildForPopWindowEntity<List<D>> mDatum : mData) {
                    D d = (D) new TwoLevelForPopWindowEntity();
                    d.setId("");
                    d.setNo("");
                    d.setName("不限");
                    List<D> list = mDatum.getChildren();
                    if (list == null){
                        list = new ArrayList<>();
                    }
                    list.add(0,d);
                    mDatum.setChildren(list);
                }
                mOneLevel.setDatas(mData);
            }
        }catch (Exception e){
            e.fillInStackTrace();
        }
    }

    @Override
    protected List<TwoLevelChildForPopWindowEntity<List<D>>> getData() {
        return null;
    }

    public interface TwoLevelClick<D> {
        void oneClick(TwoLevelChildForPopWindowEntity<List<D>> d, int position);

        void twoClick(D d,int position);

        default void reset(){

        }
    }
}
